class_name MapGenerator
extends RefCounted

# 地图生成参数
const FLOORS_PER_ACT = 15
const NODES_PER_FLOOR = 7
const ELITE_FLOORS = [4, 8, 12]
const BOSS_FLOOR = 15

# 节点类型权重
const NODE_WEIGHTS = {
	MapNode.NodeType.COMBAT: 60,
	MapNode.NodeType.REST: 15,
	MapNode.NodeType.SHOP: 10,
	MapNode.NodeType.EVENT: 10,
	MapNode.NodeType.TREASURE: 5
}

var random_seed: int
var rng: RandomNumberGenerator

func _init(seed_value: int = 0):
	if seed_value == 0:
		seed_value = Time.get_unix_time_from_system()
	random_seed = seed_value
	rng = RandomNumberGenerator.new()
	rng.seed = random_seed

func generate_map() -> Array[Array]:
	var map: Array[Array] = []
	
	# 生成每一层
	for floor in range(1, FLOORS_PER_ACT + 1):
		var floor_nodes = _generate_floor(floor)
		map.append(floor_nodes)
	
	return map

func _generate_floor(floor_num: int) -> Array[MapNode]:
	var nodes: Array[MapNode] = []
	
	# 确定节点数量（每层7个节点）
	var node_count = NODES_PER_FLOOR
	
	# 特殊楼层处理
	if floor_num in ELITE_FLOORS:
		# 精英楼层：1个精英节点，其他随机
		nodes.append(_create_elite_node(floor_num))
		node_count -= 1
	elif floor_num == BOSS_FLOOR:
		# Boss楼层：只有Boss节点
		nodes.append(_create_boss_node(floor_num))
		return nodes
	
	# 生成其他节点
	for i in range(node_count):
		var node_type = _choose_node_type(floor_num)
		var node = _create_node(node_type, floor_num, i)
		nodes.append(node)
	
	# 设置节点位置和连接
	_setup_node_positions(nodes)
	_setup_node_connections(nodes, floor_num)
	
	return nodes

func _choose_node_type(floor_num: int) -> MapNode.NodeType:
	# 根据楼层调整权重
	var weights = NODE_WEIGHTS.duplicate()
	
	# 前几层增加休息和商店概率
	if floor_num <= 3:
		weights[MapNode.NodeType.REST] += 10
		weights[MapNode.NodeType.SHOP] += 5
	
	# 后几层增加战斗概率
	if floor_num >= 10:
		weights[MapNode.NodeType.COMBAT] += 20
		weights[MapNode.NodeType.TREASURE] += 5
	
	# 随机选择节点类型
	var total_weight = 0
	for weight in weights.values():
		total_weight += weight
	
	var random_value = rng.randi_range(1, total_weight)
	var current_weight = 0
	
	for node_type in weights.keys():
		current_weight += weights[node_type]
		if random_value <= current_weight:
			return node_type
	
	return MapNode.NodeType.COMBAT

func _create_node(node_type: MapNode.NodeType, floor_num: int, index: int) -> MapNode:
	var node_id = "floor_" + str(floor_num) + "_node_" + str(index)
	return MapNode.new(node_id, node_type, floor_num, index, floor_num)

func _create_elite_node(floor_num: int) -> MapNode:
	var node_id = "floor_" + str(floor_num) + "_elite"
	return MapNode.new(node_id, MapNode.NodeType.ELITE, floor_num, 3, floor_num)

func _create_boss_node(floor_num: int) -> MapNode:
	var node_id = "floor_" + str(floor_num) + "_boss"
	return MapNode.new(node_id, MapNode.NodeType.BOSS, floor_num, 3, floor_num)

func _setup_node_positions(nodes: Array[MapNode]):
	# 设置节点的X位置，让它们分布在地图上
	var node_count = nodes.size()
	for i in range(node_count):
		nodes[i].x_position = i

func _setup_node_connections(nodes: Array[MapNode], floor_num: int):
	# 设置节点之间的连接关系
	# 第一层所有节点都可访问
	if floor_num == 1:
		for node in nodes:
			node.is_accessible = true
		return
	
	# 其他层根据前一层设置可访问性
	# 这里简化处理，实际应该有更复杂的路径算法
	for node in nodes:
		# 随机决定是否可访问（简化版）
		if rng.randf() < 0.7:  # 70%概率可访问
			node.is_accessible = true

func get_random_seed() -> int:
	return random_seed

func set_seed(new_seed: int):
	random_seed = new_seed
	rng.seed = new_seed
